import requests
import os
import re
#2021-05-06  14:23 by 炸天帮成员
#修改自所有的_6_和所有的_7_
#以下内容主体构造来自_7_bilibili暴走大事件第8季.py
#在_7_bilibili暴走大事件终极版前奏曲.py的字典基础上，删除了key    'url2'
#key   'url2' 将在addUrl2ToBil_info(site_main_url)以元组的形式被生成，然后插入至字典中。
#  目的：由于B站的请求头是一直在变的，近期懒得手动添加请求头，就尝试写了这个版本。
#--------------------------------------------------------------------------------------------
#   这样以后每次只要修改字典中的main_url即可，只要B站不从根本上替换资源的请求方式以及资源格式。
#   因为听说以前B站的资源还使用过.ts格式以及.flv格式，目前是.m4s格式，将mp4的视频轨道和音频轨道给拆成了2个文件。
#   缺陷:由于本文件第一次请求依旧是通过url进行的(其实是我懒得改，这是第3次迭代版本)，故以后每次新增时也需要往字典中添加url，
#   即新增一集就必须需要添加main_url和url。url来自于F12->network->带有30032.m4s或30280.m4s字样的东西->Request Url。
#   如果失效了，要么是main_url(即每一集的网站地址)失效了，要么就是B站把网站资源请求方式又改变了。
#   目前资源请求的方式是GET,status_code=206,content-type=xxx/mp4
#--------------------------------------------------------------------------------------------
#   关于新增资源:
#           需要在bil_info字典中，添加'080X':{'main_url':'...','30032':{'url':'...','headers':{...}},'30280':{'url':'...','headers':{...}},'name':...}
#           关于main_url和url是啥，请看上方注解。
#--------------------------------------------------------------------------------------------
#  心得：
#  1.每个网站在各个时期的反爬机制是不一样的，重点还是要看F12的network中资源是如何被请求到的。
#  2.然后尝试着去.html文件中找到有没有相关的请求踪迹，简单点的就直接放在src中，稍微复杂点的放在JS中。
#  3.如果有踪迹则追巡，没有的话，那就随便啦，反正兴趣使然。
#  4.找到请求用的地址后，就是使用正则筛选以及构造请求头文件。
#  5.至于验证码啥的，要么用自动化，要么图像识别，要么就是直接控制键鼠。
#  总结：找资源请求方式，模拟请求方式。
#--------------------------------------------------------------------------------------------
#  以下为bil_info字典定义
USER_AGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400'
RANGE='bytes=0-'
NAME='暴走大事件第8季'
REFERER="https://www.bilibili.com/bangumi/play/ep391229?bsource=baidu_aladdin"
REFERER2='https://www.bilibili.com/video/BV1Xh411r7vi'
bil_info={'0801':
                    {'main_url':'https://www.bilibili.com/video/BV1Ap4y1W7zQ',
                     '30032':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/77/46/296694677/296694677_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620223074&gen=playurlv2&os=coso1bv&oi=2062930117&trid=1cd58392d4ab44eea5c90ce8706b2c9du&platform=pc&upsig=ece26be6f4d2ce02b2485baa4458c5df&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                      'headers':{'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400',
                                                        'Range':RANGE+'42247058',
                                                         'referer':REFERER,
                                                         'connection':'keep-alive'}},
                      '30280':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/77/46/296694677/296694677_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620223074&gen=playurlv2&os=coso1bv&oi=2062930117&trid=1cd58392d4ab44eea5c90ce8706b2c9du&platform=pc&upsig=024a2cd058a510a2608ba631d1f1650c&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                        'headers':{'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400',
                                                        'Range':RANGE+'25855816',
                                                         'referer':REFERER,
                                                         'connection':'keep-alive'}},
                      'name':NAME+'01'},      #0801 END
                '0802':
                    {'main_url':'https://www.bilibili.com/video/BV1Xh411r7vi',
                     '30032':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/80/36/303583680/303583680_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620216385&gen=playurlv2&os=kodobv&oi=2062930117&trid=3f1c93c6289b40e2bb3d524374ad5683u&platform=pc&upsig=234fa4d985634e247ed731189694ec84&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=1,3&agrr=0&logo=40000000',
                                'headers':{'user-agent':USER_AGENT,
                                                'Range':RANGE+'74860850',
                                                'referer':REFERER2,
                                                'connection':'keep-alive'}},
                     '30280':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/80/36/303583680/303583680_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620216385&gen=playurlv2&os=coso1bv&oi=2062930117&trid=3f1c93c6289b40e2bb3d524374ad5683u&platform=pc&upsig=cc1b21b85e60b292aebbf55c87c46cfb&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'headers':{'user-agent':USER_AGENT,
                                                 'Range':RANGE+'45129074',
                                                 'referer':REFERER2,
                                                 'connection':'keep-alive'}},
                     'name':NAME+'02'},      #0802 END
                 '0803':
                      {'main_url':'https://www.bilibili.com/video/BV1RX4y1V7jE',
                          '30032':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/44/57/308915744/308915744_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620217109&gen=playurlv2&os=kodobv&oi=2062930117&trid=fbccbbd85cbb4260a54503be8d821b43u&platform=pc&upsig=ca917ceafd6ac1b49d282cab6b271687&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',                                   
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'92987023',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/44/57/308915744/308915744_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620217109&gen=playurlv2&os=kodobv&oi=2062930117&trid=fbccbbd85cbb4260a54503be8d821b43u&platform=pc&upsig=0f73368693a23c7261d7eadd21329f82&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=1,3&agrr=0&logo=40000000',                            
                                'headers':{'user-agent':USER_AGENT,
                                'Range':RANGE+'62357573',
                                'referer':REFERER,
                                'connection':'keep-alive'}},
                       'name':NAME+'03'},       #0803 END
                  '0804':
                      {'main_url':'https://www.bilibili.com/video/BV1S54y187yL',
                       '30032':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/71/52/315315271/315315271_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620219345&gen=playurlv2&os=coso1bv&oi=2062930117&trid=8385ed90c51e48f09a0502274ef29587u&platform=pc&upsig=92645b8c8a8c398af461d5ff03e05a32&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'headers':{'user-agent':USER_AGENT,
                                'Range':RANGE+'105458956',
                                'referer':REFERER2,
                                'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorkodoo1.bilivideo.com/upgcxcode/71/52/315315271/315315271_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620219345&gen=playurlv2&os=kodoo1bv&oi=2062930117&trid=8385ed90c51e48f09a0502274ef29587u&platform=pc&upsig=5d5211f43e73b4f4577c322545944159&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'headers':{'user-agent':USER_AGENT,
                                'Range':RANGE+'60210381',
                                'referer':REFERER2,
                                'connection':'keep-alive'}},
                       'name':NAME+'04'},       #0804 END
                  '0805':
                      {'main_url':'https://www.bilibili.com/video/BV1cy4y147mU',
                          '30032':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/67/34/321493467/321493467_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620220640&gen=playurlv2&os=vcache&oi=2062930117&trid=f75a19b22fb04ca681f126ff95efd0a8u&platform=pc&upsig=a0a159df45d3097d257199d1ed3b20bb&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=3711&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'94129921',
                                    'referer':REFERER2,
                                    'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/67/34/321493467/321493467_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620220829&gen=playurlv2&os=vcache&oi=2062930117&trid=3b4c2df4a0ff4372a055dd5bcfdca862u&platform=pc&upsig=fb085ac302a2bb02e48ea9c327f9b8c8&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6594&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'57892497',
                                    'referer':REFERER2,
                                    'connection':'keep-alive'}},
                       'name':NAME+'05'},       #0805 END
                  '0806':
                      {'main_url':'https://www.bilibili.com/video/BV1Zy4y1W7sb',
                          '30032':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/41/44/327594441/327594441_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620222102&gen=playurlv2&os=kodobv&oi=2062930117&trid=d6654f2ce8bc473290f1be696a4ba274u&platform=pc&upsig=15daa1cf7d7ef9f92d85c8e22162a779&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'73890133',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/41/44/327594441/327594441_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620222102&gen=playurlv2&os=kodobv&oi=2062930117&trid=d6654f2ce8bc473290f1be696a4ba274u&platform=pc&upsig=da1bb8fb58b06cb7e53e1eb5bff6d439&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'40968938',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       'name':NAME+'06'},       #0806 END
                  '0807':
                      {'main_url':'https://www.bilibili.com/video/BV1Z5411g7by',
                       '30032':{'url':'https://cn-zjjh4-dx-v-09.bilivideo.com/upgcxcode/43/92/334629243/334629243-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620310010&gen=playurlv2&os=vcache&oi=2062932273&trid=0807f963d34c4cfbaeb6c64c75d9af4bu&platform=pc&upsig=8a60a959a266342a88652139372d1e2b&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6594&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'128540587',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       '30280':{'url':'https://cn-zjjh4-dx-v-07.bilivideo.com/upgcxcode/43/92/334629243/334629243-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620310010&gen=playurlv2&os=vcache&oi=2062932273&trid=0807f963d34c4cfbaeb6c64c75d9af4bu&platform=pc&upsig=1448aa399dc76f2b3b608c77351d3d94&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6592&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                   'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'51931094',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       'name':NAME+'07'},   #0807 END
              }
#--------------------------------------------------------------------------------------------
mainFileDir=f'.\\pyBilibili\\'
if os.path.exists(mainFileDir):
    pass
else:
    os.mkdir(mainFileDir)
#--------------------------------------------------------------------------------------------
site_main_url_HTML_FileAddress=mainFileDir+'bili.txt'
def addUrl2ToBil_info(site_main_url):
    '''
    函数功能：用于生成 往bil_info字典中添加'url2' 的动态请求源
    原理：清洗存在于JS中的Request Url数据，分别为30032和30280
    特征:  30032.m4s   30280.m4s
    传入参数：  来自bil_info字典 的 main_url
    输出参数：  为一个包含清洗后的30032和30280的Request Url地址，并以元组的方式输出。
    使用方式：  将在__main__处的while循环中往bil_info插入'url'。
    '''
    url=site_main_url
    headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
         'referer':site_main_url,}
    r=requests.get(url,headers=headers)
    print('status_code=',r.status_code)
    print('content-type',r.headers['content-type'])
    f=open(site_main_url_HTML_FileAddress,'wb')
    f.write(r.content)
    f.close()
    f=open(site_main_url_HTML_FileAddress,'r',encoding='utf-8')
    fileContent_html=f.read()
    f.close()
    print(fileContent_html)

    pttn='\"id\":32\S*baseUrl\S*-30032.m4s\S*logo=\d+\",'
    result_30032=re.findall(pttn,fileContent_html)
    print('result_30032长度为=',len(result_30032),'\n',result_30032)
    str_result_30032=str(result_30032)
    print('转换成字符串的result为：',str_result_30032)
    saigo_result_30032=str_result_30032.split('\",')
    print('saigo_result长度为=',len(saigo_result_30032),'\n',saigo_result_30032[0])
    final_pttn='https?:\/\/\S*logo=\d+'
    final_result_30032=re.findall(final_pttn,str(saigo_result_30032[0]))
    print('最终爬到的30032url=',final_result_30032,type(final_result_30032),'\n')
    last_pttn='https:\/\/\S*\/upgcxcode\/'
    last_change='https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/'
    #last_result=re.sub(last_pttn,last_change,zaku_url)
    last_result_30032=re.sub(last_pttn,last_change,final_result_30032[0])
    print('last_result_30032=',last_result_30032,type(last_result_30032))

    pttn2='\"id\":30280\S*-30280.m4s\S*logo=\d+\",'
    result_30280=re.findall(pttn2,fileContent_html)
    print('result_30280=',result_30280)
    str_result_30280=str(result_30280)
    saigo_result_30280=str_result_30280.split('\",')
    final_pttn='https?:\/\/\S*logo=\d+'
    final_result_30280=re.findall(final_pttn,str(saigo_result_30280[0]))
    print('最终爬到的30280url=',final_result_30280,type(final_result_30280),'\n')
    last_pttn='https:\/\/\S*\/upgcxcode\/'
    last_change='https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/'
    last_result_30280=re.sub(last_pttn,last_change,final_result_30280[0])
    print('last_result_30280=',last_result_30280,type(last_result_30280))

    return (last_result_30032,last_result_30280)

##bil_info['0801']['30032']['url2']=addUrl2ToBil_info(bil_info['0801']['main_url'])[0]#用于测试生成0801的30032的url2
##bil_info['0801']['30280']['url2']=addUrl2ToBil_info(bil_info['0801']['main_url'])[1]#用于测试生成0801的30280的url2
##print(bil_info)
#--------------------------------------------------------------------------------------------
def getSource(Https,Headers,Address):
        '''
        函数功能：用于获取请求到的网络资源，并保存到本地。
        传入参数：Https 即 Request URL
                        Headers 即 Response Headers & Request Headers
                        Address 即 最终文件保存所在地
        返回参数：fileContent 即 从网络获取到的资源的2进制数据，将在changeUrl(FileContent,FileTypeTag,Address)函数中被用于判断文件大小。
        '''
        r=requests.get(Https,headers=Headers)
        f=open(Address,'wb')
        f.write(r.content)
        f.close()
        f=open(Address,"rb")
        fileContent=f.read()
        f.close()
        return fileContent

def changeUrl(FileContent,FileTypeTag,Address):
    '''
    函数功能：判断保存至本地的文件的大小，并返回是否下载成功标志位。
    传入参数：FileContent 即 2进制文件的数据
                    FileTypeTag 即 用于切换显示文字 的判断用
                    Address 即 被判断的本地文件的所在文件地址。
    返回参数：
                    一个字符串。当文件过小时，返回'下载失败'，以便后续再次使用getSource(Https,Headers,Address)进行请求源的切换；否则就是下载成功。
    '''
    if FileTypeTag == 'video':
        text='视频部分'
    elif FileTypeTag == 'voice':
        text='音频部分'
    if len(FileContent)<40960:
        print(Address+'疑似无效文件！')
        os.remove(Address)
        print(Address+'已删除！')
        return 'DLD False!'
    else:
        print(Address+text+'已经下载完成')
        return 'DLD Success!'

#--------------------------------------------------------------------------------------------
#以下为__main__入口
def main():
    MOV_INDEX=801
    MOV_INDEX_MAX=800+len(bil_info)  #以后修改此处即可替换最大集数
    while MOV_INDEX<=MOV_INDEX_MAX :
        videoFile_m4s=bil_info['0'+str(MOV_INDEX)]['name']+'_video'+'.mp4'
        if os.path.exists(mainFileDir+videoFile_m4s):
            print('已存在'+videoFile_m4s)
            pass
        else:
            videoFile=getSource(bil_info['0'+str(MOV_INDEX)]['30032']['url'],bil_info['0'+str(MOV_INDEX)]['30032']['headers'],mainFileDir+videoFile_m4s)
            DLD_Flag=changeUrl(videoFile,'video',mainFileDir+videoFile_m4s)
            if DLD_Flag=='DLD False!':
                print("尝试更换请求源...")
                try:
                    bil_info['0'+str(MOV_INDEX)]['30032']['url2']=addUrl2ToBil_info(bil_info['0'+str(MOV_INDEX)]['main_url'])[0]
                    videoFile=getSource(bil_info['0'+str(MOV_INDEX)]['30032']['url2'],bil_info['0'+str(MOV_INDEX)]['30032']['headers'],mainFileDir+videoFile_m4s)
                    DLD_Flag=changeUrl(videoFile,'video',mainFileDir+videoFile_m4s)
                    if DLD_Flag=='DLD False!':
                        print("尝试失败，请新增请求源!")
                    else:
                        print("更换成功！")
                except KeyError:#因为bil_info字典里的条目有些没有设置'url2'替补源，以后添加'urlxx'时也是一样，到时候此处的try需要修改一下
                    print(videoFile_m4s+'不存在替补源，请新增请求源!')
                    pass

        voiceFile_m4s=bil_info['0'+str(MOV_INDEX)]['name']+'_voice'+'.mp4'
        if os.path.exists(mainFileDir+voiceFile_m4s):
            print('已存在'+voiceFile_m4s)
            pass
        else:
            voiceFile=getSource(bil_info['0'+str(MOV_INDEX)]['30280']['url'],bil_info['0'+str(MOV_INDEX)]['30280']['headers'],mainFileDir+voiceFile_m4s)
            DLD_Flag=changeUrl(voiceFile,'voice',mainFileDir+voiceFile_m4s)
            if DLD_Flag=='DLD False!':
                print("尝试更换请求源...")
                try:
                    bil_info['0'+str(MOV_INDEX)]['30280']['url2']=addUrl2ToBil_info(bil_info['0'+str(MOV_INDEX)]['main_url'])[1]
                    voiceFile=getSource(bil_info['0'+str(MOV_INDEX)]['30280']['url2'],bil_info['0'+str(MOV_INDEX)]['30280']['headers'],mainFileDir+voiceFile_m4s)
                    DLD_Flag=changeUrl(voiceFile,'voice',mainFileDir+voiceFile_m4s)
                    if DLD_Flag=='DLD False!':
                        print("尝试失败，请新增请求源!")
                    else :
                        print("更换成功!")
                except KeyError:#因为bil_info字典里的条目有些没有设置'url2'替补源，以后添加'urlxx'时也是一样，到时候此处的try需要修改一下
                    print(voiceFile_m4s+'不存在替补源，请新增请求源!')
                    pass
        MOV_INDEX+=1
    if os.path.exists(site_main_url_HTML_FileAddress):
        os.remove(site_main_url_HTML_FileAddress)
    else:
        pass
#--------------------------------------------------------------------------------------------
if __name__=='__main__':
    main()
